using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using IFWiki.Models;

namespace IFWiki.Controllers
{
    public class GameController : Controller
    {
        //
        // GET: /Game/
        public ActionResult Index(string name)
        {
            var game = new Game(name);
            return View(game);
        }

        public ActionResult ViewSource(string name)
        {
            var game = new Game(name);
            return View(game);
        }

        public ActionResult ListRevisions(string name)
        {
            //TempData["game"] = new Game(gameName);
            //return RedirectToAction("List", "History", new { historyItemName = "game" });
            return RedirectToAction("List", "History", new { name = name, innerType = "Game" });
        }

        //
        // GET: /Game/Edit/5
        public ActionResult Edit(string name)
        {
            var game = new Game(name);
            
            return View(game);
        }

        //
        // POST: /Game/Edit/5
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string name, FormCollection collection)
        {
            var game = new Game(name);

            try
            {
                string reason = collection["Reason"]; 
                game.AboutGame = collection["AboutGame"];
                game.GameStyle = collection["GameStyle"];
                game.OnPlaceEnterJS = collection["OnPlaceEnterJS"];
                game.StartingGamePlaceName = collection["StartingGamePlaceName"];
                game.StartingPlayerJson = collection["StartingPlayerJson"];
                //Defer: implement features for game.Tags 
                game.Save(reason, User.Identity.Name);

                return RedirectToAction("Index", new { name = name });
            }
            catch
            {
                return View(game);
            }
        }

        public ActionResult ViewRevision(string name, DateTime revisedAt)
        {
            string revertedBy = User.Identity.Name;
            var game = Game.LoadRevision(name, revisedAt);
            return View(game);
        }
    }
}